﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Routing;
using System.Web.UI;
using MvcCarConsume.App_GlobalResources;

namespace MvcCarConsume.HtmlHelpers
{
    public static class SitePathHelper
    {
        public static void RenderNavMenu(this HtmlHelper html)
        {
            HtmlTextWriter writer = new HtmlTextWriter(html.ViewContext.Writer);
            // RenderRecursive(writer, SiteMap.RootNode);
            PrintSiteMapPath(writer);
        }

        private static void PrintSiteMapPath(HtmlTextWriter writer)
        {
            System.Diagnostics.Debug.WriteLine(HttpContext.Current.Request.Url.PathAndQuery + "!");
            if (SiteMap.CurrentNode != null)
            {
                PrintNaviInfo(writer, SiteMap.CurrentNode);
            }
            else
            {
                string url = GetCurrentUrl();
                flag = false;
                PrintNaviInfo(writer, GetNodeByUrl(url, SiteMap.RootNode));
            }
        }

        private static string GetCurrentUrl()
        {
            string url = HttpContext.Current.Request.Url.PathAndQuery;
            
            if (char.IsLetter(url[url.Count() - 1]))
            {
                url += "/";
            }
            else
            {
                while (char.IsNumber(url[url.Count() - 1]))
                {
                    url = url.Remove(url.Count() - 1);
                }
            }

            url = url.Remove(0, 1);
            while (url[0] != '/')
            {
                url = url.Remove(0, 1);
            }
            if (url.StartsWith("/view/index"))
                return "/view/index/";
            return url;
        }

        static bool flag = false;
        private static SiteMapNode GetNodeByUrl(string url, SiteMapNode node)
        {
            if (node.Url.ToLower() == url.ToLower())
            {
                flag = true;
                return node;
            }
            foreach (SiteMapNode item in node.ChildNodes)
            {
                SiteMapNode rNode = GetNodeByUrl(url, item);
                if (flag)
                    return rNode;
            }
            return null;
        }

        private static void PrintNaviInfo(HtmlTextWriter writer, SiteMapNode node)
        {
            if (node == null)
                return;
            SiteMapNode smn = node;
            string html = GetTextByKey(smn.Title);
            smn = smn.ParentNode;
            while (smn != null)
            {
                html = " → " + html;
                html = "<a href=\"" + smn.Url + "\">" + GetTextByKey(smn.Title) + "</a>" + html;
                smn = smn.ParentNode;
            }
           // html = string.Format("<div class=\"navibar\">{0}</div>", html);
            writer.Write(html);
        }

        private static string GetTextByKey(string key)
        {
            return (string)HttpContext.GetGlobalResourceObject("Resources", key, System.Globalization.CultureInfo.CurrentUICulture);
        }

        //private static void RenderRecursive(HtmlTextWriter writer, SiteMapNode node)
        //{
        //    System.Diagnostics.Debug.WriteLine(SiteMap.CurrentNode);
        //    if (SiteMap.CurrentNode == node) // Highlight visitor's location
        //        writer.RenderBeginTag(HtmlTextWriterTag.B); // Render as bold text
        //    else
        //    {
        //        // Render as link
        //        writer.AddAttribute(HtmlTextWriterAttribute.Href, node.Url);
        //        writer.RenderBeginTag(HtmlTextWriterTag.A);
        //    }
        //    writer.Write(node.Title);
        //    writer.RenderEndTag();
        //    // Render children
        //    if (node.ChildNodes.Count > 0)
        //    {
        //        writer.RenderBeginTag(HtmlTextWriterTag.Ul);
        //        foreach (SiteMapNode child in node.ChildNodes)
        //        {
        //            writer.RenderBeginTag(HtmlTextWriterTag.Li);
        //            RenderRecursive(writer, child);
        //            writer.RenderEndTag();
        //        }
        //        writer.RenderEndTag();
        //    }
        //}
    }

}